/*
 * 
 *方法的整理与重构
 *
 *
 */
//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','opinion_list');

/*controller部分*/
//主要操作controller
authApp.controller("opinionTabController",function($scope,$http,$getQueryString,$location,$splice){
	//针对此页面需要的参数 should edit
	$scope.groupId=$getQueryString("groupId");
	$scope.itemId=$getQueryString("taskId");
	var taskType=$getQueryString("taskType");
	if(taskType=="0"){
		$scope.taskTypeName="学前教育";
	}else if(taskType=="1"){
		$scope.taskTypeName="小学教育";
	}else if(taskType=="2"){
		$scope.taskTypeName="中学教育";
	}else{
		$scope.taskTypeName="未选择";
	}
	$scope.taskName=unUnicode($getQueryString("taskName"));
	$scope.schoolName=unUnicode($getQueryString("schoolName"));
	$scope.addList=[{'sk_teacher':"",'sk_zc':"",'sk_age':"",'sk_reason':""}];
	
	//**********主体方法	
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	var models="";
	var keys="";
	var values="";
	//添加机构信息
	$scope.addFirstZ=function(){
		if(window.confirm("是否确定保存？")){
			/*models+="a,a,a,a";
			keys+="sk_teacher,sk_zc,sk_age,sk_reason";
			values+=$scope.sk_teacher+","+$scope.sk_zc+","+$scope.sk_age+","+$scope.sk_reason;*/
			
			//遍历获取A模块的数据
			angular.forEach($scope.addList,function(data,index,array){
				if(index=="0"){
					models+="a,a,a,a";
					keys+="sk_teacher"+(index+1)+",sk_zc"+(index+1)+",sk_age"+(index+1)+",sk_reason"+(index+1);
					values+=data.sk_teacher+","+data.sk_zc+","+data.sk_age+","+data.sk_reason;
				}else{
					models+=",a,a,a,a";
					keys+=",sk_teacher"+(index+1)+",sk_zc"+(index+1)+",sk_age"+(index+1)+",sk_reason"+(index+1);
					values+=","+data.sk_teacher+","+data.sk_zc+","+data.sk_age+","+data.sk_reason;
				}										
			});
			
			//遍历获取B模块的数据
			angular.forEach($scope.list,function(data,index,array){
				var fp="b"+(index+1);
				angular.forEach(data.list,function(data,index,array){
					models+=",b";
					keys+=","+fp+""+(index+1);
					values+=","+data.getValue;
				})
				models+=",b,b";
				keys+=","+fp+""+"a"+","+fp+""+"b";
				values+=","+data.getLevel+","+data.mainQuestion;
			});
			
			//获取c模块的数据
			models+=",c,c,c";
			keys+=",opinion01,opinion02,opinion03";
			values+=","+$scope.opinion01+","+$scope.opinion02+","+$scope.opinion03;
			var rJson={groupId:$scope.groupId,itemId:$scope.itemId,keys:keys,values:values,modes:models,}
			$http({
				method:'post',
				url:apis.saveAuthIinspectionOpinionsLists,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					alert("保存成功！");
					/*$scope.queryAuthIinspectionOpinionsLists();*/
				}else if(response.code=="111111"){
					parent.location.href=pages.login;
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				
			})
		}
	}
	
	//根据一级指标查询对应学校的二级指标
	$scope.queryTwoItemByOneItem=function(){
		var rJson={'groupId':$scope.groupId,'itemId':$scope.itemId};
		$http({
			method:'get',
			url:apis.queryTwoItemByOneItem,
			params:rJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.list=response.data;
				$scope.queryAuthIinspectionOpinionsLists(response.data);
			}else if(response.code=="111111"){
				parent.location.href=pages.login;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	
	//查询专家某一学校一级指标分项考察意见表数据
	$scope.queryAuthIinspectionOpinionsLists=function(frameData){
		var rJson={'groupId':$scope.groupId,'itemId':$scope.itemId};
		$http({
			method:'get',
			url:apis.queryAuthIinspectionOpinionsLists,
			params:rJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.dataList=response.data;
				if($scope.dataList==""){	
					angular.forEach(frameData,function(array,index,data){						
						array.getLevel="";
						array.mainQuestion="";
						angular.forEach(array.list,function(arraylist,indexa,dataa){
							arraylist.getValue="";
						})
					})
				}else{
					angular.forEach(frameData,function(array,index,data){						
						var fp="b"+(index+1)+"";						
						array.getLevel=eval("$scope.dataList.b."+fp+"a");
						array.mainQuestion=eval("$scope.dataList.b."+fp+"b");											
						angular.forEach(array.list,function(arraylist,indexa,dataa){
							arraylist.getValue=eval("$scope.dataList.b."+fp+(indexa+1));
						})
					});
					
					var jslength=0;
					for(var js2 in $scope.dataList.a){
						jslength++;
					}

					for(var index=0;index<jslength/4;index++){
						if(index>0){
							var jsons={'sk_teacher':"",'sk_zc':"",'sk_age':"",'sk_reason':""};
							$scope.addList.push(jsons);
							$scope.addList[index].sk_teacher=eval("$scope.dataList.a.sk_teacher"+(index+1));
							$scope.addList[index].sk_reason=eval("$scope.dataList.a.sk_reason"+(index+1));
							$scope.addList[index].sk_age=eval("$scope.dataList.a.sk_age"+(index+1));
							$scope.addList[index].sk_zc=eval("$scope.dataList.a.sk_zc"+(index+1));
						}else{
							$scope.addList[index].sk_teacher=eval("$scope.dataList.a.sk_teacher"+(index+1));
							$scope.addList[index].sk_reason=eval("$scope.dataList.a.sk_reason"+(index+1));
							$scope.addList[index].sk_age=eval("$scope.dataList.a.sk_age"+(index+1));
							$scope.addList[index].sk_zc=eval("$scope.dataList.a.sk_zc"+(index+1));	
						}
					}						
					
					$scope.opinion01=$scope.dataList.c.opinion01;
					$scope.opinion02=$scope.dataList.c.opinion02;
					$scope.opinion03=$scope.dataList.c.opinion03;
				}
			}else if(response.code=="111111"){
				parent.location.href=pages.login;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	
	$scope.turnTo=function(pid){
		location.href="opinion_list.html";
	}
	
	$scope.addUl=function(){
		var addSingleList={'sk_teacher':"",'sk_zc':"",'sk_age':"",'sk_reason':""};
		$scope.addList.push(addSingleList);		
	}
	
	function unUnicode(str)
	   {
		return unescape(str.replace(/\\/g, "%"));
	   }
	
	
	
	$scope.queryTwoItemByOneItem();
   
})

